package info.sgu.ttdt.adapter;

import info.sgu.ttdt.model.BookMark;
import info.sgu.ttdt.model.DiemChiTiet;
import info.sgu.ttdt.model.DiemTongQuat;
import info.sgu.ttdt.model.HocKy;
import info.sgu.ttdt.model.LichThi;
import info.sgu.ttdt.model.TaiKhoan;
import info.sgu.ttdt.model.ThoiKhoaBieu;
import info.sgu.ttdt.model.Tuan;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

	// Logcat tag
	private static final String LOG = "DatabaseHelper";

	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "DB";

	// Table Names
	private static final String TABLE_DIEMCHITIET = "DiemChiTiet";
	private static final String TABLE_DIEMTONGQUAT = "DiemTongQuat";
	private static final String TABLE_HOCKY = "HocKy";
	private static final String TABLE_TUAN = "Tuan";
	private static final String TABLE_TAIKHOAN = "TaiKhoan";
	private static final String TABLE_LICHTHI = "LichThi";
	private static final String TABLE_THOIKHOABIEU = "ThoiKhoaBieu";
	private static final String TABLE_BOOKMARK = "Bookmark";

	// Common column names
	private static final String KEY_ID = "id";
	private static final String KEY_MASINHVIEN = "masinhvien";
	private static final String KEY_MAHOCKY = "hockyid";
	private static final String KEY_MATUAN = "tuanid";

	// DiemChiTiet Table - column names
	private static final String KEY_MAMONHOC = "mamonhoc";
	private static final String KEY_TENMONHOC = "tenmonhoc";
	private static final String KEY_SOTINCHI = "sotinchi";
	private static final String KEY_PHANTRAMKIEMTRA = "phantramkiemtra";
	private static final String KEY_PHANTRAMTHI = "phantramthi";
	private static final String KEY_DIEMKIEMTRA = "diemkiemtra";
	private static final String KEY_DIEMTHILANMOT = "diemthilanmot";
	private static final String KEY_DIEMTONGKETHEMUOI = "diemtongkethemuoi";
	private static final String KEY_DIEMTONGKETDIEMCHU = "diemtongketdiemchu";

	// LichThi Table - column names
	private static final String KEY_GHEPTHI = "ghepthi";
	private static final String KEY_TOTHI = "tothi";
	private static final String KEY_SOLUONG = "soluong";
	private static final String KEY_NGAYTHI = "ngaythi";
	private static final String KEY_GIOBATDAU = "giobatdau";
	private static final String KEY_SOPHUT = "sophut";
	private static final String KEY_PHONG = "phong";
	private static final String KEY_HINHTHUCTHI = "hinhthucthi";

	// DiemTongQuat Table - column names
	private static final String KEY_DIEMTRUNGBINHHOCKYHEMUOI = "diemtrungbinhhockyhemuoi";
	private static final String KEY_DIEMTRUNGBINHHOCKYHEBON = "diemtrungbinhhockyhebon";
	private static final String KEY_DIEMTRUNGBINHTICHLUYHEMUOI = "diemtrungbinhtichluyhemuoi";
	private static final String KEY_DIEMTRUNGBINHTICHLUYHEBON = "diemtrungbinhtichluyhebon";
	private static final String KEY_SOTINCHIDAT = "sotinchidat";
	private static final String KEY_SOTINCHITICHLUY = "sotinchitichluy";
	private static final String KEY_DIEMTRUNGBINHRENLUYENHOCKY = "diemtrungbinhrenluyenhocky";
	private static final String KEY_PHANLOAIDTBRENLUYENHOCKY = "phanloaidtbrenluyenhocky";

	// HocKy Table - column names
	private static final String KEY_TENHOCKY = "tenhocky";
	private static final String KEY_LOAIHOCKY = "loaihocky";

	// TaiKhoan Table - column names
	private static final String KEY_TENSINHVIEN = "tensinhvien";
	private static final String KEY_LOP = "lop";
	private static final String KEY_NGANH = "nganh";
	private static final String KEY_KHOA = "khoa";
	private static final String KEY_HEDAOTAO = "hedaotao";
	private static final String KEY_KHOAHOC = "khoahoc";
	private static final String KEY_PASS = "pass";
	
	// Bookmark Table - column names
	private static final String KEY_LOAI = "loai";

	// Tuan Table - column names
	private static final String KEY_TENTUAN = "tentuan";

	// ThoiKhoaBieu Table - column names
	private static final String KEY_PHONGHOC = "phonghoc";
	private static final String KEY_THU = "thu";
	private static final String KEY_TIETBATDAU = "tietbatdau";
	private static final String KEY_SOTIET = "sotiet";
	private static final String KEY_GIANGVIEN = "giangvien";

	// Table Create Statements
	// ThoiKhoaBieu table create statement
	private static final String CREATE_TABLE_THOIKHOABIEU = "CREATE TABLE "
			+ TABLE_THOIKHOABIEU + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_MAMONHOC + " TEXT," + KEY_TENMONHOC + " TEXT," + KEY_PHONGHOC
			+ " TEXT," + KEY_THU + " TEXT," + KEY_TIETBATDAU + " TEXT,"
			+ KEY_SOTIET + " TEXT," + KEY_GIANGVIEN + " TEXT," + KEY_LOP
			+ " TEXT," + KEY_MATUAN + " INTEGER," + KEY_MAHOCKY + " INTEGER,"
			+ KEY_MASINHVIEN + " TEXT)";

	// LichThi table create statement
	private static final String CREATE_TABLE_LICHTHI = "CREATE TABLE "
			+ TABLE_LICHTHI + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_MAMONHOC + " TEXT," + KEY_TENMONHOC + " TEXT," + KEY_GHEPTHI
			+ " TEXT," + KEY_TOTHI + " TEXT," + KEY_SOLUONG + " TEXT,"
			+ KEY_NGAYTHI + " TEXT," + KEY_GIOBATDAU + " TEXT," + KEY_SOPHUT
			+ " TEXT," + KEY_PHONG + " TEXT," + KEY_HINHTHUCTHI + " TEXT,"
			+ KEY_MAHOCKY + " INTEGER," + KEY_MASINHVIEN + " TEXT)";

	// HocKy table create statement
	private static final String CREATE_TABLE_TUAN = "CREATE TABLE "
			+ TABLE_TUAN + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TENTUAN
			+ " TEXT, " + KEY_MASINHVIEN + " TEXT, " + KEY_MAHOCKY
			+ " INTEGER)";
	
	// Bookmark table create statement
	private static final String CREATE_TABLE_BOOKMARK = "CREATE TABLE "
			+ TABLE_BOOKMARK + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_MASINHVIEN
			+ " TEXT, " + KEY_TENSINHVIEN + " TEXT, " + KEY_LOAI
			+ " TEXT)";

	// TaiKhoan table create statement
	private static final String CREATE_TABLE_TAIKHOAN = "CREATE TABLE "
			+ TABLE_TAIKHOAN + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_MASINHVIEN + " TEXT," + KEY_TENSINHVIEN + " TEXT," + KEY_LOP
			+ " TEXT," + KEY_NGANH + " TEXT," + KEY_KHOA + " TEXT,"
			+ KEY_HEDAOTAO + " TEXT," + KEY_KHOAHOC + " TEXT," + KEY_PASS + " TEXT)";

	// DiemChiTiet table create statement
	private static final String CREATE_TABLE_DIEMCHITIET = "CREATE TABLE "
			+ TABLE_DIEMCHITIET + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_MAMONHOC + " TEXT," + KEY_TENMONHOC + " TEXT," + KEY_SOTINCHI
			+ " TEXT," + KEY_PHANTRAMKIEMTRA + " TEXT," + KEY_PHANTRAMTHI
			+ " TEXT," + KEY_DIEMKIEMTRA + " TEXT," + KEY_DIEMTHILANMOT
			+ " TEXT," + KEY_DIEMTONGKETHEMUOI + " TEXT,"
			+ KEY_DIEMTONGKETDIEMCHU + " TEXT," + KEY_MAHOCKY + " INTEGER,"
			+ KEY_MASINHVIEN + " TEXT)";

	// DiemTongQuat table create statement
	private static final String CREATE_TABLE_DIEMTONGQUAT = "CREATE TABLE "
			+ TABLE_DIEMTONGQUAT + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_DIEMTRUNGBINHHOCKYHEMUOI + " TEXT,"
			+ KEY_DIEMTRUNGBINHHOCKYHEBON + " TEXT,"
			+ KEY_DIEMTRUNGBINHTICHLUYHEMUOI + " TEXT,"
			+ KEY_DIEMTRUNGBINHTICHLUYHEBON + " TEXT," + KEY_SOTINCHIDAT
			+ " TEXT," + KEY_SOTINCHITICHLUY + " TEXT,"
			+ KEY_DIEMTRUNGBINHRENLUYENHOCKY + " TEXT,"
			+ KEY_PHANLOAIDTBRENLUYENHOCKY + " TEXT," + KEY_MAHOCKY
			+ " INTEGER," + KEY_MASINHVIEN + " TEXT)";

	// HocKy table create statement
	private static final String CREATE_TABLE_HOCKY = "CREATE TABLE "
			+ TABLE_HOCKY + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
			+ KEY_TENHOCKY + " TEXT, " + KEY_MASINHVIEN + " TEXT, "
			+ KEY_LOAIHOCKY + " TEXT)";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		// creating required tables
		db.execSQL(CREATE_TABLE_DIEMCHITIET);
		db.execSQL(CREATE_TABLE_DIEMTONGQUAT);
		db.execSQL(CREATE_TABLE_HOCKY);
		db.execSQL(CREATE_TABLE_TUAN);
		db.execSQL(CREATE_TABLE_TAIKHOAN);
		db.execSQL(CREATE_TABLE_LICHTHI);
		db.execSQL(CREATE_TABLE_THOIKHOABIEU);
		db.execSQL(CREATE_TABLE_BOOKMARK);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// on upgrade drop older tables
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIEMCHITIET);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIEMTONGQUAT);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_HOCKY);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TUAN);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAIKHOAN);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LICHTHI);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_THOIKHOABIEU);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKMARK);

		// create new tables
		onCreate(db);
	}

	
	// ------------------------ "Bookmark" table methods ----------------//

		/*
		 * Creating a Bookmark
		 */
		public long createBookmark(BookMark bm) {
			SQLiteDatabase db = this.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(KEY_MASINHVIEN, bm.getMasinhvien());
			values.put(KEY_TENSINHVIEN, bm.getTensinhvien());
			values.put(KEY_LOAI, bm.getLoai());
			// insert row
			long t_id = db.insert(TABLE_BOOKMARK, null, values);
			return t_id;
		}

		/*
		 * getting check Bookmark
		 */
		public boolean checkBookmark(String masinhvien, String loai) {
			String selectQuery = "SELECT  * FROM " + TABLE_BOOKMARK + " WHERE "
					+ KEY_MASINHVIEN + " = '" + masinhvien + "' AND " + KEY_LOAI
					+ " = '" + loai + "'";
			Log.e(LOG, selectQuery);
			int count = 0;
			SQLiteDatabase db = this.getReadableDatabase();
			Cursor c = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (c.moveToFirst()) {
				do {
					count++;
				} while (c.moveToNext());
			}
			if (count > 0)
				return true;
			else
				return false;
		}

		/*
		 * getting all Bookmark
		 */
		public List<BookMark> getBookmarks() {
			List<BookMark> bms = new ArrayList<BookMark>();
			String selectQuery = "SELECT  * FROM " + TABLE_BOOKMARK;
			Log.e(LOG, selectQuery);

			SQLiteDatabase db = this.getReadableDatabase();
			Cursor c = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (c.moveToFirst()) {
				do {
					BookMark bm = new BookMark();
					bm.setId(c.getInt(c.getColumnIndex(KEY_ID)));
					bm.setMasinhvien(c.getString(c.getColumnIndex(KEY_MASINHVIEN)));
					bm.setTensinhvien(c.getString(c.getColumnIndex(KEY_TENSINHVIEN)));
					bm.setLoai(c.getString(c.getColumnIndex(KEY_LOAI)));
					// adding to Tuan list
					bms.add(bm);
				} while (c.moveToNext());
			}

			return bms;
		}

		/*
		 * Deleting a Bookmark
		 */
		public void deleteBookmark(String masinhvien, String loai) {
			SQLiteDatabase db = this.getWritableDatabase();
			db.delete(TABLE_BOOKMARK, KEY_MASINHVIEN + " = '" + masinhvien + "' AND " + KEY_LOAI + " = '" + loai +"'", null);
		}

	
	
	// ------------------------ "Tuan" table methods ----------------//

	/*
	 * Creating a Tuan
	 */
	public long createTuan(Tuan t, String masinhvien, int hockyid) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_TENTUAN, t.getTentuan());
		values.put(KEY_MASINHVIEN, masinhvien);
		values.put(KEY_MAHOCKY, hockyid);
		// insert row
		long t_id = db.insert(TABLE_TUAN, null, values);
		return t_id;
	}

	/*
	 * getting check Tuan
	 */
	public boolean checkTuan(String masinhvien, int hockyid) {
		String selectQuery = "SELECT  * FROM " + TABLE_TUAN + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "' AND " + KEY_MAHOCKY
				+ " = '" + hockyid + "'";
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
		if (count > 0)
			return true;
		else
			return false;
	}

	/*
	 * getting all Tuan
	 */
	public List<Tuan> getTuans(String masinhvien, int hockyid) {
		List<Tuan> ts = new ArrayList<Tuan>();
		String selectQuery = "SELECT  * FROM " + TABLE_TUAN + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "' AND " + KEY_MAHOCKY
				+ " = '" + hockyid + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				Tuan t = new Tuan();
				t.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				t.setTentuan(c.getString(c.getColumnIndex(KEY_TENTUAN)));
				// adding to Tuan list
				ts.add(t);
			} while (c.moveToNext());
		}

		return ts;
	}

	/*
	 * Deleting all Tuan
	 */
	public void deleteTuan(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_TUAN, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

	// ------------------------ "ThoiKhoaBieu" table methods ----------------//

	/*
	 * Creating a ThoiKhoaBieu
	 */
	public long createThoiKhoaBieu(ThoiKhoaBieu tkb, int tuanid, int hockyid,
			String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_MAMONHOC, tkb.getMamonhoc());
		values.put(KEY_TENMONHOC, tkb.getTenmonhoc());
		values.put(KEY_PHONGHOC, tkb.getPhonghoc());
		values.put(KEY_THU, tkb.getThu());
		values.put(KEY_TIETBATDAU, tkb.getTietbatdau());
		values.put(KEY_SOTIET, tkb.getSotiet());
		values.put(KEY_GIANGVIEN, tkb.getGiangvien());
		values.put(KEY_LOP, tkb.getLop());
		values.put(KEY_MATUAN, tuanid);
		values.put(KEY_MAHOCKY, hockyid);
		values.put(KEY_MASINHVIEN, masinhvien);

		// insert row
		long tkb_id = db.insert(TABLE_THOIKHOABIEU, null, values);
		return tkb_id;
	}

	/*
	 * getting all ThoiKhoaBieu
	 */
	public List<ThoiKhoaBieu> getAllThoiKhoaBieus(int tuanid, int hockyid,
			String masinhvien, String thu) {
		List<ThoiKhoaBieu> tkbs = new ArrayList<ThoiKhoaBieu>();
		String selectQuery = "SELECT  * FROM " + TABLE_THOIKHOABIEU + " WHERE "
				+ KEY_MATUAN + " = '" + tuanid + "' AND " + KEY_MAHOCKY
				+ " = '" + hockyid + "' AND " + KEY_MASINHVIEN + " = '"
				+ masinhvien + "' AND " + KEY_THU + " = '"
				+ thu + "' " + "ORDER BY " + KEY_THU + " , "
				+ KEY_TIETBATDAU + " ASC";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				ThoiKhoaBieu tkb = new ThoiKhoaBieu();
				tkb.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				tkb.setMamonhoc(c.getString(c.getColumnIndex(KEY_MAMONHOC)));
				tkb.setTenmonhoc(c.getString(c.getColumnIndex(KEY_TENMONHOC)));
				tkb.setPhonghoc(c.getString(c.getColumnIndex(KEY_PHONGHOC)));
				tkb.setThu(c.getString(c.getColumnIndex(KEY_THU)));
				tkb.setTietbatdau(c.getString(c.getColumnIndex(KEY_TIETBATDAU)));
				tkb.setSotiet(c.getString(c.getColumnIndex(KEY_SOTIET)));
				tkb.setGiangvien(c.getString(c.getColumnIndex(KEY_GIANGVIEN)));
				tkb.setLop(c.getString(c.getColumnIndex(KEY_LOP)));

				// adding to ThoiKhoaBieu list
				tkbs.add(tkb);
			} while (c.moveToNext());
		}

		return tkbs;
	}

	/*
	 * getting check ThoiKhoaBieu
	 */
	public boolean checkThoiKhoaBieu(String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_THOIKHOABIEU + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
		if (count > 0)
			return true;
		else
			return false;
	}

	/*
	 * Deleting all ThoiKhoaBieu
	 */
	public void deleteThoiKhoaBieu(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_THOIKHOABIEU, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

	// ------------------------ "LichThi" table methods ----------------//

	/*
	 * Creating a LichThi
	 */
	public long createLichThi(LichThi lt, int hockyid, String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_MAMONHOC, lt.getMamonhoc());
		values.put(KEY_TENMONHOC, lt.getTenmonhoc());
		values.put(KEY_GHEPTHI, lt.getGhepthi());
		values.put(KEY_TOTHI, lt.getTothi());
		values.put(KEY_SOLUONG, lt.getSoluong());
		values.put(KEY_NGAYTHI, lt.getNgaythi());
		values.put(KEY_GIOBATDAU, lt.getGiobatdau());
		values.put(KEY_SOPHUT, lt.getSophut());
		values.put(KEY_PHONG, lt.getPhong());
		values.put(KEY_HINHTHUCTHI, lt.getHinhthucthi());
		values.put(KEY_MAHOCKY, hockyid);
		values.put(KEY_MASINHVIEN, masinhvien);

		// insert row
		long lt_id = db.insert(TABLE_LICHTHI, null, values);
		return lt_id;
	}

	/*
	 * getting all LichThi
	 */
	public List<LichThi> getAllLichThis(int hockyid, String masinhvien) {
		List<LichThi> lts = new ArrayList<LichThi>();
		String selectQuery = "SELECT  * FROM " + TABLE_LICHTHI + " WHERE "
				+ KEY_MAHOCKY + " = '" + hockyid + "' AND " + KEY_MASINHVIEN
				+ " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				LichThi lt = new LichThi();
				lt.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				lt.setMamonhoc(c.getString(c.getColumnIndex(KEY_MAMONHOC)));
				lt.setTenmonhoc(c.getString(c.getColumnIndex(KEY_TENMONHOC)));
				lt.setGhepthi(c.getString(c.getColumnIndex(KEY_GHEPTHI)));
				lt.setTothi(c.getString(c.getColumnIndex(KEY_TOTHI)));
				lt.setSoluong(c.getString(c.getColumnIndex(KEY_SOLUONG)));
				lt.setNgaythi(c.getString(c.getColumnIndex(KEY_NGAYTHI)));
				lt.setGiobatdau(c.getString(c.getColumnIndex(KEY_GIOBATDAU)));
				lt.setSophut(c.getString(c.getColumnIndex(KEY_SOPHUT)));
				lt.setPhong(c.getString(c.getColumnIndex(KEY_PHONG)));
				lt.setHinhthucthi(c.getString(c.getColumnIndex(KEY_HINHTHUCTHI)));

				// adding to LichThi list
				lts.add(lt);
			} while (c.moveToNext());
		}

		return lts;
	}

	/*
	 * getting check LichThi
	 */
	public boolean checkLichThi(String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_LICHTHI + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
		if (count > 0)
			return true;
		else
			return false;
	}

	/*
	 * Deleting all LichThi
	 */
	public void deleteLichThi(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_LICHTHI, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

	// ------------------------ "TaiKhoan" table methods ----------------//

	/*
	 * Creating a TaiKhoan
	 */
	public long createTaiKhoan(TaiKhoan tk) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_MASINHVIEN, tk.getMasinhvien());
		values.put(KEY_TENSINHVIEN, tk.getTensinhvien());
		values.put(KEY_LOP, tk.getLop());
		values.put(KEY_NGANH, tk.getNganh());
		values.put(KEY_KHOA, tk.getKhoa());
		values.put(KEY_HEDAOTAO, tk.getHedaotao());
		values.put(KEY_KHOAHOC, tk.getKhoahoc());
		values.put(KEY_PASS, tk.getPass());

		// insert row
		long tk_id = db.insert(TABLE_TAIKHOAN, null, values);
		return tk_id;
	}

	/*
	 * getting all TaiKhoan
	 */
	public List<TaiKhoan> getAllTaiKhoans() {
		List<TaiKhoan> tks = new ArrayList<TaiKhoan>();
		String selectQuery = "SELECT  * FROM " + TABLE_TAIKHOAN;
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				TaiKhoan tk = new TaiKhoan();
				tk.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				tk.setMasinhvien(c.getString(c.getColumnIndex(KEY_MASINHVIEN)));
				tk.setTensinhvien(c.getString(c.getColumnIndex(KEY_TENSINHVIEN)));
				tk.setLop(c.getString(c.getColumnIndex(KEY_LOP)));
				tk.setNganh(c.getString(c.getColumnIndex(KEY_NGANH)));
				tk.setKhoa(c.getString(c.getColumnIndex(KEY_KHOA)));
				tk.setHedaotao(c.getString(c.getColumnIndex(KEY_HEDAOTAO)));
				tk.setKhoahoc(c.getString(c.getColumnIndex(KEY_KHOAHOC)));

				// adding to TaiKhoan list
				tks.add(tk);
			} while (c.moveToNext());
		}

		return tks;
	}

	/*
	 * getting TaiKhoan
	 */
	public TaiKhoan getTaiKhoan(String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_TAIKHOAN + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c != null)
			c.moveToFirst();
		TaiKhoan tk = new TaiKhoan();
		tk.setId(c.getInt(c.getColumnIndex(KEY_ID)));
		tk.setMasinhvien(c.getString(c.getColumnIndex(KEY_MASINHVIEN)));
		tk.setTensinhvien(c.getString(c.getColumnIndex(KEY_TENSINHVIEN)));
		tk.setLop(c.getString(c.getColumnIndex(KEY_LOP)));
		tk.setNganh(c.getString(c.getColumnIndex(KEY_NGANH)));
		tk.setKhoa(c.getString(c.getColumnIndex(KEY_KHOA)));
		tk.setHedaotao(c.getString(c.getColumnIndex(KEY_HEDAOTAO)));
		tk.setKhoahoc(c.getString(c.getColumnIndex(KEY_KHOAHOC)));

		return tk;

	}

	/*
	 * check TaiKhoan
	 */
	public boolean checkTaiKhoan(String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_TAIKHOAN + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
		if (count > 0)
			return true;
		else
			return false;

	}
	
	/*
	 * check TaiKhoan
	 */
	public int numberTaiKhoan() {
		String selectQuery = "SELECT  * FROM " + TABLE_TAIKHOAN;
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
			return count;

	}

	/*
	 * Deleting all DiemChiTiet
	 */
	public void deleteTaiKhoan(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_TAIKHOAN, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

	// ------------------------ "HocKy" table methods ----------------//

	/*
	 * Creating a HocKy
	 */
	public long createHocKy(HocKy hk, String masinhvien, String type) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_TENHOCKY, hk.getTenhocky());
		values.put(KEY_MASINHVIEN, masinhvien);
		values.put(KEY_LOAIHOCKY, type);
		// insert row
		long hk_id = db.insert(TABLE_HOCKY, null, values);
		return hk_id;
	}

	/*
	 * getting all HocKy
	 */
	public List<HocKy> getHocKys(String masinhvien, String type) {
		List<HocKy> hks = new ArrayList<HocKy>();
		String selectQuery = "SELECT  * FROM " + TABLE_HOCKY + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "' AND "
				+ KEY_LOAIHOCKY + " = '" + type + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				HocKy hk = new HocKy();
				hk.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				hk.setTenhocky(c.getString(c.getColumnIndex(KEY_TENHOCKY)));

				// adding to DiemChiTiet list
				hks.add(hk);
			} while (c.moveToNext());
		}

		return hks;
	}

	/*
	 * Deleting all HocKy
	 */
	public void deleteHocKy(String masinhvien, String type) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_HOCKY, KEY_MASINHVIEN + " = '" + masinhvien + "' AND "
				+ KEY_LOAIHOCKY + " = '" + type + "'", null);
	}

	// ------------------------ "DiemTongQuat" table methods ----------------//

	/*
	 * Creating a DiemTongQuat
	 */
	public long createDiemTongQuat(DiemTongQuat tq, int hockyid,
			String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_DIEMTRUNGBINHHOCKYHEMUOI,
				tq.getDiemtrungbinhhockyhemuoi());
		values.put(KEY_DIEMTRUNGBINHHOCKYHEBON, tq.getDiemtrungbinhhockyhebon());
		values.put(KEY_DIEMTRUNGBINHTICHLUYHEMUOI,
				tq.getDiemtrungbinhtichluyhemuoi());
		values.put(KEY_DIEMTRUNGBINHTICHLUYHEBON,
				tq.getDiemtrungbinhtichluyhebon());
		values.put(KEY_SOTINCHIDAT, tq.getSotinchidat());
		values.put(KEY_SOTINCHITICHLUY, tq.getSotinchitichluy());
		values.put(KEY_DIEMTRUNGBINHRENLUYENHOCKY,
				tq.getDiemtrungbinhrenluyenhocky());
		values.put(KEY_PHANLOAIDTBRENLUYENHOCKY,
				tq.getPhanloaidtbrenluyenhocky());
		values.put(KEY_MAHOCKY, hockyid);
		values.put(KEY_MASINHVIEN, masinhvien);

		// insert row
		long tq_id = db.insert(TABLE_DIEMTONGQUAT, null, values);
		return tq_id;
	}

	/*
	 * getting all DiemTongQuat
	 */
	public DiemTongQuat getDiemTQs(int hockyid, String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_DIEMTONGQUAT + " WHERE "
				+ KEY_MAHOCKY + " = '" + hockyid + "' AND " + KEY_MASINHVIEN
				+ " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c != null)
			c.moveToFirst();
		DiemTongQuat tq = new DiemTongQuat();
		tq.setId(c.getInt(c.getColumnIndex(KEY_ID)));
		tq.setDiemtrungbinhhockyhemuoi(c.getString(c
				.getColumnIndex(KEY_DIEMTRUNGBINHHOCKYHEMUOI)));
		tq.setDiemtrungbinhhockyhebon(c.getString(c
				.getColumnIndex(KEY_DIEMTRUNGBINHHOCKYHEBON)));
		tq.setDiemtrungbinhtichluyhemuoi(c.getString(c
				.getColumnIndex(KEY_DIEMTRUNGBINHTICHLUYHEMUOI)));
		tq.setDiemtrungbinhtichluyhebon(c.getString(c
				.getColumnIndex(KEY_DIEMTRUNGBINHTICHLUYHEBON)));
		tq.setSotinchidat(c.getString(c.getColumnIndex(KEY_SOTINCHIDAT)));
		tq.setSotinchitichluy(c.getString(c.getColumnIndex(KEY_SOTINCHITICHLUY)));
		tq.setDiemtrungbinhrenluyenhocky(c.getString(c
				.getColumnIndex(KEY_DIEMTRUNGBINHRENLUYENHOCKY)));
		tq.setPhanloaidtbrenluyenhocky(c.getString(c
				.getColumnIndex(KEY_PHANLOAIDTBRENLUYENHOCKY)));

		return tq;

	}

	/*
	 * Deleting all DiemTongQuat
	 */
	public void deleteDiemTQ(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_DIEMTONGQUAT, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

	// ------------------------ "DiemChiTiet" table methods ----------------//

	/*
	 * Creating a DiemChiTiet
	 */
	public long createDiemChiTiet(DiemChiTiet ct, int hockyid, String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_MAMONHOC, ct.getMamonhoc());
		values.put(KEY_TENMONHOC, ct.getTenmonhoc());
		values.put(KEY_SOTINCHI, ct.getSotinchi());
		values.put(KEY_PHANTRAMKIEMTRA, ct.getPhantramkiemtra());
		values.put(KEY_PHANTRAMTHI, ct.getPhantramthi());
		values.put(KEY_DIEMKIEMTRA, ct.getDiemkiemtra());
		values.put(KEY_DIEMTHILANMOT, ct.getDiemthilanmot());
		values.put(KEY_DIEMTONGKETHEMUOI, ct.getDiemtongkethemuoi());
		values.put(KEY_DIEMTONGKETDIEMCHU, ct.getDiemtongketdiemchu());
		values.put(KEY_MAHOCKY, hockyid);
		values.put(KEY_MASINHVIEN, masinhvien);

		// insert row
		long ct_id = db.insert(TABLE_DIEMCHITIET, null, values);
		return ct_id;
	}

	/*
	 * getting all DiemChiTiet
	 */
	public List<DiemChiTiet> getAllDiems(int hockyid, String masinhvien) {
		List<DiemChiTiet> cts = new ArrayList<DiemChiTiet>();
		String selectQuery = "SELECT  * FROM " + TABLE_DIEMCHITIET + " WHERE "
				+ KEY_MAHOCKY + " = '" + hockyid + "' AND " + KEY_MASINHVIEN
				+ " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				DiemChiTiet ct = new DiemChiTiet();
				ct.setId(c.getInt(c.getColumnIndex(KEY_ID)));
				ct.setMamonhoc(c.getString(c.getColumnIndex(KEY_MAMONHOC)));
				ct.setTenmonhoc(c.getString(c.getColumnIndex(KEY_TENMONHOC)));
				ct.setSotinchi(c.getString(c.getColumnIndex(KEY_SOTINCHI)));
				ct.setPhantramkiemtra(c.getString(c
						.getColumnIndex(KEY_PHANTRAMKIEMTRA)));
				ct.setPhantramthi(c.getString(c.getColumnIndex(KEY_PHANTRAMTHI)));
				ct.setDiemkiemtra(c.getString(c.getColumnIndex(KEY_DIEMKIEMTRA)));
				ct.setDiemthilanmot(c.getString(c
						.getColumnIndex(KEY_DIEMTHILANMOT)));
				ct.setDiemtongkethemuoi(c.getString(c
						.getColumnIndex(KEY_DIEMTONGKETHEMUOI)));
				ct.setDiemtongketdiemchu(c.getString(c
						.getColumnIndex(KEY_DIEMTONGKETDIEMCHU)));

				// adding to DiemChiTiet list
				cts.add(ct);
			} while (c.moveToNext());
		}

		return cts;
	}

	/*
	 * getting check DiemChiTiet
	 */
	public boolean checkDiem(String masinhvien) {
		String selectQuery = "SELECT  * FROM " + TABLE_DIEMCHITIET + " WHERE "
				+ KEY_MASINHVIEN + " = '" + masinhvien + "'";
		Log.e(LOG, selectQuery);
		int count = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		if (c.moveToFirst()) {
			do {
				count++;
			} while (c.moveToNext());
		}
		if (count > 0)
			return true;
		else
			return false;
	}

	/*
	 * Deleting all DiemChiTiet
	 */
	public void deleteDiem(String masinhvien) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_DIEMCHITIET, KEY_MASINHVIEN + " = ?",
				new String[] { masinhvien });
	}

}
